package cn.feizhu.aimarket.ai.workflow.node;

import cn.feizhu.aimarket.ai.workflow.model.constant.WorkflowControlConstant;
import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.action.EdgeAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 智能判断调度器
 * 根据智能判断结果决定下一步流程
 *
 * @author feizhu
 * @since 2025/10/16
 */
public class SmartJudgeDispatcher implements EdgeAction {

    private static final Logger logger = LoggerFactory.getLogger(SmartJudgeDispatcher.class);


    @Override
    public String apply(OverAllState state) {
        logger.info("=== 智能判断调度器开始执行 ===");
        // TODO 这里创建一个智能路由客户端
        try {
            // 获取判断结果
            String judgeResult = (String) state.value(WorkflowControlConstant.JUDGE_RESULT)
                    .orElse(WorkflowControlConstant.BRANCH_FAIL);

            logger.info("判断结果: {}", judgeResult);

            if (WorkflowControlConstant.BRANCH_PASS.equals(judgeResult)) {
                logger.info("判断通过，进入指令处理节点");
                return WorkflowControlConstant.BRANCH_PASS;
            } else {
                logger.info("判断失败，结束工作流");
                return WorkflowControlConstant.BRANCH_FAIL;
            }

        } catch (Exception e) {
            logger.error("智能判断调度器执行异常", e);
            return WorkflowControlConstant.BRANCH_FAIL;
        } finally {
            logger.info("=== 智能判断调度器执行完成 ===");
        }
    }
}
